import type { JobLogListParamsType, JobLogType, JobType } from './model'
import type { SpColumnsType } from '@/components/SpTable/type'
import { Tag } from 'ant-design-vue'
import { addDialog } from '@/components/SpDialog/index'
import { formatDate } from '@/utils/dateUtils'
import { getJobLogListAPI } from './api'

export function useJobLog() {
  // 日志列
  const jobLogColumn: SpColumnsType[] = [
    {
      title: '任务名称',
      dataIndex: 'jobName',
      align: 'center'
    },
    {
      title: '任务分组',
      dataIndex: 'jobGroup',
      align: 'center'
    },
    {
      title: '日志信息',
      dataIndex: 'jobMessage',
      type: 'slot',
      align: 'center'
    },
    {
      title: '调用目标',
      dataIndex: 'invokeTarget',
      align: 'center'
    },
    {
      title: '状态',
      dataIndex: 'status',
      cellRenderer: (record: JobLogType) => {
        if (record.status === 0) {
          return <Tag color="red">异常</Tag>
        } else {
          return <Tag color="green">成功</Tag>
        }
      },
      width: 100,
      align: 'center'
    },
    {
      title: '执行时间',
      dataIndex: 'startTime',
      cellRenderer: (record: JobLogType) => <span>{formatDate(record.startTime)}</span>,
      align: 'center'
    },
    {
      title: '异常信息',
      dataIndex: 'exceptionInfo',
      type: 'slot',
      align: 'center'
    }
  ]

  // 日志弹窗
  const open = ref<boolean>(false)

  // 查询参数
  const jobLogQueryParams = ref<JobLogListParamsType>({
    pageNum: 1,
    pageSize: 10
  })
  // 表格数据
  const jobLogList = ref<JobLogType[]>([])
  // 表格加载
  const jobLogLoading = ref<boolean>(false)
  // 选中项
  const jobLogSelectedRowKeys = ref<number[]>([])

  // 获取定时任务调度日志
  function getJobLogList() {
    jobLogLoading.value = true
    getJobLogListAPI(jobLogQueryParams.value)
      .then((res) => {
        jobLogList.value = res.data.rows
        jobLogQueryParams.value.total = res.data.total
      })
      .finally(() => {
        jobLogLoading.value = false
      })
  }

  // 弹窗变化
  const afterOpenChange = (bool: boolean) => {
    !bool && clearData()
  }

  // 分页
  function jobLogPaginationChange(paging: number, size: number) {
    jobLogQueryParams.value.pageNum = paging
    jobLogQueryParams.value.pageSize = size
    getJobLogList()
  }

  //搜索
  function jobLogSearch() {
    jobLogQueryParams.value.pageNum = 1
    getJobLogList()
  }

  // 清空搜索
  function jobLogClear() {
    jobLogQueryParams.value = Object.assign({
      pageNum: 1,
      pageSize: 10
    })
    getJobLogList()
  }

  // 打开日志详情
  function openJobLogModal(content: string) {
    addDialog({
      title: '详情',
      fullScreen: true,
      contentRenderer: () => (
        <div style={{ padding: '15px', lineHeight: '1.5' }}>{content ? content : '暂无...'}</div>
      )
    })
  }

  // 批量删除
  function jobLogBatchDeleteChange() {}

  // 选中项
  const JobLogSelectChange = (keys: number[]) => {
    console.log('selectedRowKeys changed: ', keys)
    jobLogSelectedRowKeys.value = keys
  }

  // 清空数据
  function clearData() {
    jobLogList.value = []
    jobLogQueryParams.value = Object.assign({
      pageNum: 1,
      pageSize: 10,
      status: 0
    })
    jobLogLoading.value = false
  }

  // 显示日志
  const showDrawer = (job: JobType) => {
    jobLogQueryParams.value.jobName = job.jobName
    getJobLogList()
    open.value = true
  }
  return {
    open,
    jobLogQueryParams,
    jobLogList,
    jobLogLoading,
    jobLogColumn,
    afterOpenChange,
    jobLogPaginationChange,
    jobLogSearch,
    jobLogClear,
    showDrawer,
    jobLogSelectedRowKeys,
    jobLogBatchDeleteChange,
    JobLogSelectChange,
    openJobLogModal
  }
}
